﻿#coding: utf-8

require 'sinatra/jsonp'

put '/link' do
	begin
		data = JSON.parse(request.body.string)
	rescue JSON::ParserError
		400
	end

	if data.nil? or !data.has_key?('source') or !data.has_key?('target') then
		400
	else
		l = Link.create(
			:source_id => data['source'],
			:target_id => data['target']
		)
		l.save
		200
	end
end

delete '/link/:source' do
	link = Link.get(params[:source], params[:target])
	if link != nil then
		if link.destroy then
			200
		else
			400
		end
	else
		404
	end
end
